import pygame
import random
import math

#窗口初始化
pygame.init()
pygame.display.init()
screen = pygame.display.set_mode((800,600))
#设置icon
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('飞机大作战')

#pygame.display.set_icon('aaa')
#窗口关闭标志
win_close_flag =True
#导入背景图片
bg_image = pygame.image.load('bg.png')
#导入战斗机的图片
player = pygame.image.load('player.png')
#加载背景音乐
pygame.mixer.music.load('bg.wav')
#-1循环播放
pygame.mixer.music.play(-1)
#爆炸声音
pung_music = pygame.mixer.Sound('exp.wav')
#发射声音
shoot_music = pygame.mixer.Sound('laser.wav')
#飞机初始时的x轴坐标
playerx =400
#飞机初始时的y轴坐标
playery =500
#飞机初始时的移动速度
playerStep = 0
#得分
score=0
#定义文字
score_font =pygame.font.Font('ttf/arialbd.ttf',32)
game_font =pygame.font.Font('ttf/arialbd.ttf',66)

#定义敌人enemy类
class Enemy():
    def __init__(self):
        self.ex =random.randint(100,600)
        self.ey =random.randint(30,200)
        if self.ex%2 ==0:
            self.step =3
        else:
            self.step =-3
        self.enemy_image = pygame.image.load('enemy.png')
    #敌人移动
    def move(self):
        if self.ex <=0:
            self.step = 3
            self.ey += 4    #靠近边界，才下降
        if self.ex >=776:
            self.step = -3
            self.ey += 4   #靠近边界，才下降

        self.ex += self.step

#定义子弹Bullet类
class Bullet():
    def __init__(self,x,y):
        self.x =x
        self.y =y
        self.step = 6
        self.bullet_image = pygame.image.load('bullet.png')
    #子弹移动，Y轴减少，x轴不变
    def move(self):
        self.y -= self.step
#定义子弹集合
bullets=[]
def show_bullet():
    for bullet in bullets:
        screen.blit(bullet.bullet_image, (bullet.x,bullet.y))
        bullet.move()

#定义敌人集合
enemys=[]
#生成敌人
for i in range(6):
    enemys.append(Enemy())
#显示敌人
def show_enemy():
    for enemy in enemys:
        screen.blit(enemy.enemy_image, (enemy.ex, enemy.ey))
        #敌人移动
        enemy.move()


#战机边界判定
def player_is_out_screen():
    global playerx
    if playerx <=0:
        playerx =0
    elif playerx >= 736:#屏幕宽度 - 飞机的宽度 ：800-64 =736
        playerx = 736

#射中判断
def hit_enemy():
    global score
    #循环遍历所有的敌人，和子弹
    for enemy in enemys:
        for bullet in bullets:
            lenX = bullet.x - enemy.ex
            lenY = bullet.y - enemy.ey
            if math.sqrt(lenX*lenX+lenY*lenY) <=33:
                enemys.remove(enemy)
                bullets.remove(bullet)
                #播放爆炸的声音
                pung_music.play()
                #分数+1
                score +=1
                print('score:',score)

#显示文字
def show_font():
    global score
    font =score_font.render(f'Score:{score}',True,(0,255,255))
    screen.blit(font,(10,10))

#如果敌人进入飞机的区域，则游戏结束
def show_game_over():
    #global win_close_flag
    for enemy in enemys:
        if enemy.ey >= playery +20:
            #游戏结束
            #win_close_flag =False
            #显示game over
            gane_over=game_font.render('GAME OVER!',True,(255,255,255))
            screen.blit(gane_over,(150,250))
#一直显示窗口
while win_close_flag:
    #获取事件
    events = pygame.event.get()
    #显示背景图片,设置背景图片的位置 x =0,y=0
    screen.blit(bg_image,(0,0))
    #显示战斗机
    screen.blit(player,(playerx,playery))
    #显示敌人
    show_enemy()
    #移动战斗机
    player_is_out_screen()
    #显示子弹
    show_bullet()
    #显示分数
    show_font()
    #判断游戏时否输了
    show_game_over()
    for event in events:
        #判断事件是否是退出，窗口的X 按钮
        if event.type == pygame.QUIT:
            print('游戏关闭……')
            win_close_flag =False
        if event.type == pygame.KEYDOWN:
            #print('按下按键……',event)
            #<Event(768-KeyDown {'unicode': '', 'key': 1073741903, 'mod': 0, 'scancode': 79, 'window': None})>
            if event.key ==pygame.K_RIGHT:
                playerStep = 5
            elif event.key ==pygame.K_LEFT:
                playerStep = -5
            elif event.key == pygame.K_SPACE:#按空格键
                bullets.append(Bullet(playerx+15,playery-10))
                #播放发射的声音
                shoot_music.play()

        if event.type == pygame.KEYUP:
            #print('松开按键……')
            playerStep =0
    playerx += playerStep
    #检测子弹是否射中敌人
    hit_enemy()
    #每一帧画面都要在最后刷新
    pygame.display.update()


